/*
 * @(#)HapiMllpService.java
 * Date 2013-03-30
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.hapiservice;

import ca.uhn.hl7v2.DefaultHapiContext;
import ca.uhn.hl7v2.HapiContext;

import ca.uhn.hl7v2.app.HL7Service;

import ca.uhn.hl7v2.protocol.ReceivingApplication;


/**
 *
 * @author jim
 */
public class HapiMllpService {

    /** Field description */
    private final ChainReceiverApplication receivingApplication;

    /** Field description */
    private final HL7Service server;

    /**
     * Constructs ...
     *
     *
     * @param port
     */
    public HapiMllpService(int port) {

        super();

        HapiContext context = new DefaultHapiContext();

        this.server = context.newServer(port, false);

        this.receivingApplication = new ChainReceiverApplication();
        this.server.registerApplication("*", "*", this.receivingApplication);
    }

    /**
     * Method description
     *
     *
     * @param application
     */
    public void registerApplication(ReceivingApplication application) {

        this.receivingApplication.addApplication(application);
    }

    /**
     * Method description
     *
     *
     * @param listener
     */
    public void registerListener(ReceivingApplication listener) {

        this.receivingApplication.addListener(listener);
    }

    /**
     * Method description
     *
     */
    public void shutdown() {

        this.server.stopAndWait();
    }

    /**
     * Method description
     *
     *
     * @throws InterruptedException
     */
    public void start() throws InterruptedException {

        this.server.startAndWait();
    }
}
